/*
 * @Author: w
 * @Date: 2022-02-23 22:46:43
 * @Description: 面试题32 - I. 从上到下打印二叉树
 */

// 从上到下 BFS 广度查询,利用队列先入后出

/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @return {number[]}
 */
var levelOrder = function (root) {
  if (!root) return [];
  const ans = [];
  const queue = [root];
  while (queue.length) {
    const node = queue.shift();
    ans.push(node.val);
    if (node.left) queue.push(node.left);
    if (node.right) queue.push(node.right);
  }
  return ans;
};
